package com.taillive.commons.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;



public class FileUtils {
	
	
	
	
	public static String appendFileName(String fileName,String append){
		
        if (fileName == null || fileName.equals("")) {
            return append;
        }
		
		
		String result = null;
		
        int pos = fileName.lastIndexOf(".");
        
        if (pos < 0 || pos == fileName.length() - 1) {
        	result = fileName + append;
        }
        else {
        	result =fileName.substring(0,pos)+append+ fileName.substring(pos);
        }
        return result;
		
	}
	 /**
     *  get extend name of filename
     * @param fileName String
     * @return String
     */
    public static String getExtName(String fileName) {
        if (fileName == null || fileName.equals("")) {
            return "";
        }

        int pos = fileName.lastIndexOf(".");
        if (pos < 0 || pos == fileName.length() - 1) {
            return "";
        }
        else {
            return fileName.substring(pos);
        }
    }

    /**
     * is file exist
     * @param path String
     * @return boolean
     */
    public static boolean isExist(String path) {
        File file = new File(path);
        return file.exists();
    }

    /**
     * make dirs of path
     * @param path String
     */
    public static void mkDirs(String path) {
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }



    //----------------------- delete file -----------------------

    public static void delete(File file) {
        if (file.exists()) {
            file.delete();
        }
        file = null;
    }
    

    public static void delete(String path) {
        delete(new File(path));
    }

    //----------------------- write file -----------------------

    public static void write(InputStream is, OutputStream os) {
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try {
            while ( (bytesRead = is.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.flush();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (os != null) {
                    os.close();
                    os = null;
                }
                if (is != null) {
                    is.close();
                    is = null;
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public static void write(String inputPath, String outputPath) {
        try {
            write(new FileInputStream(inputPath),
                  new FileOutputStream(outputPath));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write(File inputFile, File outputFile) {
        try {
            write(new FileInputStream(inputFile),
                  new FileOutputStream(outputFile));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write(InputStream is, File outputFile) {
        try {
            write(is, new FileOutputStream(outputFile));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write(InputStream is, String outputPath) {
        try {
            write(is, new FileOutputStream(outputPath));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    //----------------------- get file size -----------------------

    public static long getFileSize(File file) {
        if (file.exists()) {
            return file.length();
        }
        else {
            return 0;
        }
    }


    /**
     *
     * return size of new File(path)
     *
     * @param path String
     * @return long
     */

    public static long getFileSize(String path) {
        return getFileSize(new File(path));
    }


    /**
     *
     * return size as KB of new File(path);
     *
     * @param file File
     * @return int
     */


    public static int getFileSizeAsKB(String path) {
        return getFileSizeAsKB(new File(path));
    }

    public static int getSizeAsKB(long l){
        if (l != 0) {
            int size = (int) (l / 1024);
            if (size <= 0) {
                size = 1;
            }
            return size;
        }
        else {
            return 0;
        }

    }


    /**
     *
     * return size as KB of file
     *
     * @param file File
     * @return int
     */


    public static int getFileSizeAsKB(File file) {

        long l = getFileSize(file);
        return getSizeAsKB(l);

    }

   
 
    public static int getUniqueFileNo(String dir,String fileName){
    	
    	String prefix = null;
        int pos = fileName.lastIndexOf(".");
        
        if (pos < 0 || pos == fileName.length() - 1) {
        	prefix = fileName;
        }
        else {
        	prefix = fileName.substring(0,pos);
        }
    	
        
        File file = new File(dir);
        String[] files = file.list();
        int i=1;
    	for(String fs:files){
    		if(fs.startsWith(prefix)){
    			i++;
    		}
    	}
    	
    	return i;
    }





}
